# ------------------------------------------------------------------------------------------------
### Generate Table 8: Effects for never treated control
# ------------------------------------------------------------------------------------------------

# Load data 
load("../data/results/sdid_results.Rda")

# Filter and prepare data
filtered_results <- filter_sdid_results(
  sdid_results,
  outcome_filter = c("Overall Turnout", "Party Turnout"),
  party_filter = c("Opposition", "Overall Turnout", "Law and Justice (PiS)"),
  control_sample_filter = "nevertreated"
) %>%
  mutate(
    outcome = case_when(
      outcome == "Party Turnout" & party == "Opposition" ~ "Opposition Turnout",
      outcome == "Party Turnout" & party == "Law and Justice (PiS)" ~ "Government Turnout",
      TRUE ~ outcome
    ),
    outcome = factor(outcome, levels = c("Overall Turnout", "Opposition Turnout", "Government Turnout")),
    sample = ifelse(sample == "< 50km Sample", "$<$ 50km Sample", sample),
    sample = factor(sample, levels = c("$<$ 50km Sample", "Full Sample"))
  ) %>%
  arrange(outcome)

# Generate and save table
latex_table <- create_latex_table(
  filtered_results,
  group_var = "outcome",
  row_var = "sample",
  caption = "Synthetic Difference-in-Differences Estimates for Never Treated Units",
  label = "tab:sdid_never_treated",
  col_names = c("Outcome", "Sample", "Estimate (SE)", "N")
)

save_table(latex_table, "../output/tables/table8.txt")
